﻿using CATLTempUpdate.CommonLib;
using CATLTempUpdate.TcpServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace CATLTempUpdate
{
    public class AnalysisBytes
    {
        public static List<byte> recTempMTBytes = new List<byte>();
        public static List<byte[]> sndTempMTBytes = new List<byte[]>();

        public static List<byte> recSECBytes = new List<byte>();
        public static List<byte[]> sndSECBytes = new List<byte[]>();

        MyCommand mCmd = new MyCommand();

        public bool RecFEDCTdTag = true;
        public bool SndFEDCTdTag = true;

        public AnalysisBytes()
        {
        }

        public void AsyncFindFEDCCode(byte[] iToSecBuffList)
        {
            delFindFEDCCode.BeginInvoke(iToSecBuffList, null, null);
        }

        private delegate void DelFindFEDCCode(byte[] iToSecBuffList);
        DelFindFEDCCode delFindFEDCCode;
        /// <summary>
        /// 获取温度基板发送的温度报文
        /// </summary>
        /// <param name="FromTempMTBuffList">收取的温度采集的报文</param>
        /// <param name="IP">发送报文的客户端的IP</param>
        public void AnalysisTempDataCode(byte[] FromTempMTBuffList, string IP)
        {
            try
            {
                List<byte> TempList = new List<byte>();
                TempList = FromTempMTBuffList.ToList();
                while (TempList.Count > 0)
                {
                    int FirstIndex = TempList.IndexOf(126);
                    if (FirstIndex > 0)
                    {
                        TempList.RemoveRange(0, FirstIndex);
                        Log.AddLog(LogFile.Trace, ("发现不完整包:" + string.Join("-", FromTempMTBuffList.ToArray())));
                    }
                    else if (FirstIndex == 0)
                    {
                        TempList.RemoveAt(0);
                        FirstIndex = TempList.IndexOf(126);
                        List<byte> packdata = TempList.Take(FirstIndex).ToList();
                        if (packdata.Count == 0)
                        {
                            Log.AddLog(LogFile.Trace, ("发现粘包," + "报文长度为:" + FromTempMTBuffList.Length + "," + ":" + string.Join("-", FromTempMTBuffList)));
                        }
                        else
                        {
                            //反解析7E 转成的7D 5E ,7D 转成的7D 5D
                            int TargetArrayLen;
                            List<byte> TargetByteArray;
                            CRCCheck.DecryptData(packdata, packdata.Count, out TargetByteArray, out TargetArrayLen);
                            if (TargetByteArray != null)
                            {
                                if (SenseDataCollection.IfWriteLog)
                                {
                                    Log.AddLog(LogFile.Trace, ("收到报文," + "IP:" + IP + ",报文长度为:" + TargetArrayLen + "," + ":" + string.Join("-", TargetByteArray)));
                                }
                                CRCCheck crc = new CRCCheck(TargetByteArray);
                                TempList.RemoveRange(0, (FirstIndex + 1));
                            }
                        }
                    }
                    else
                    {
                        TempList.Clear();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.AddLog(LogFile.SystemError, ("收到异常报文," + "报文长度为:" + FromTempMTBuffList.Length + "," + ":" + string.Join("-", FromTempMTBuffList.ToArray()))+ex.Message);
            }

        }


        /// <summary>
        /// 获取SEC发送的温度请求
        /// </summary>
        /// <param name="FromSecBuffList"></param>
        public void FindFEDCCode(byte[] FromSecBuffList)
        {

            try
            {
                if (recSECBytes.Count > 1024 * 20000)
                {
                    recSECBytes.RemoveRange(0, recSECBytes.Count / 2);
                }

                if (FromSecBuffList.ToList().IndexOf(254) >= 0 && FromSecBuffList.ToList().IndexOf(220) >= 0 && FromSecBuffList.Length >= 6)
                {

                    recSECBytes.AddRange(FromSecBuffList);
                    string str = "";
                    for (int i = 0; i < FromSecBuffList.Length; i++)
                    {
                        str += " " + FromSecBuffList[i].ToString("X");
                    }
                    Log.AddLog(LogFile.Trace, "收到SEC报文" + str);
                }
            }
            catch (Exception ex)
            {
                Log.AddLog(LogFile.SystemError, ex.ToString());
            }

        }

        public void Dispose()
        {
            this.RecFEDCTdTag = false;
            this.SndFEDCTdTag = false;
        }

    }
}

